package com.urchin.avatarhero.questview;

import java.util.ArrayList;

import android.database.Cursor;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.urchin.avatarhero.R;
import com.urchin.avatarhero.util.SQLiteHandler;
import com.urchin.avatarhero.util.Utilities;
import com.urchin.avatarhero.util.cal.CalBlob;
import com.urchin.avatarhero.util.cal.CalendarHandler;

/**********************************************************************
AvatarHero - A task management RPG Android Application
Copyright (C) 2013  Peter Clay

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

public class CurrentQuests extends QuestView {

	@Override
	public void refresh() {
		// Set the Header Text
	     TextView textViewHeader = (TextView) findViewById(R.id.questHeader);
		 textViewHeader.setText(R.string.title_activity_CurrentQuests);
	     
	     // Display the list of available quests
		// Get the Calendar data
	     CalendarHandler calhandle = new CalendarHandler(this);
	     // Get the Event and Instance data
	     ArrayList<CalBlob> cdata = calhandle.buildCalData(calhandle.getEvents(), calhandle.getInstances());
	     ArrayList<String> elist = new ArrayList<String>();
	     Utilities util = new Utilities(this);
	     
	     SQLiteHandler dbHandler = new SQLiteHandler(getApplicationContext());
		 dbHandler.opendb();
		 Cursor rcursor = dbHandler.getCurQuests();
		
	     // Remove any entries with exp already assigned
		 rcursor.moveToFirst();	   
		 CalBlob cb = new CalBlob();
		 for(int i = 0; i < rcursor.getCount(); i++) {
			 cb.setCalid(rcursor.getInt(0));
			 cb.setEventid(rcursor.getInt(1));
			 cb.setInstanceid(rcursor.getInt(2));
			 cb.setDifficulty(rcursor.getInt(3));
			 if(cb.getDifficulty() > 0) {
				 // Find the times
				 for(CalBlob caldata : cdata) {
					 if(Utilities.isSameCalBlob(caldata, cb)) {
						 cb.setTitle(caldata.getTitle());
						 cb.setStarttime(caldata.getStarttime());
						 cb.setEndtime(caldata.getEndtime());
						 elist.add(util.convertToStrPattern(cb, "caldisp", dbHandler));		// Convert to the calendar display pattern
						 break;
					 }
				 }
			 }
			 rcursor.moveToNext();
		 }
		   
		 rcursor.close();
		 dbHandler.closedb();
		    
		 Toast.makeText(act.getApplicationContext(),"elist size: " + String.valueOf(elist.size()),Toast.LENGTH_SHORT).show();   
		    
	     // Create the adapter
	     qadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elist);
	     listView = (ListView) findViewById(R.id.questButtons);
	     listView.setAdapter(qadapter);
	     listView.setOnItemClickListener(QuestClickedHandler); 		
	}
		
	// Create a message handling object as an anonymous class.
	 private OnItemClickListener QuestClickedHandler = new OnItemClickListener() {
		 public void onItemClick(AdapterView parent, View v, int position, long id)
		 {
			 Utilities util = new Utilities(act);
			 // Display a messagebox.
			 String calstuff = (String) ((TextView)v).getText();
			 CalBlob cb = util.StrPatternToCalBlob(calstuff);
			 		     
		     // Find the proper instance
			 CalendarHandler calhandle = new CalendarHandler(act);
		     ArrayList<CalBlob> cdata = calhandle.buildCalData(calhandle.getEvents(), calhandle.getInstances());
		     for(CalBlob instance : cdata) {
		    	 if((cb.getStarttime() == instance.getStarttime()) && (cb.getTitle().equals(instance.getTitle()))) {		// If the begin times match up and the names match
		    		 cb.setCalid(instance.getCalid());
		    		 cb.setInstanceid(instance.getInstanceid());
		    		 cb.setEventid(instance.getEventid());
		    		 break;
		    	 }		    	 
		     }
		     
		     // Show the dialog
		     mStackLevel++;

		     FragmentTransaction ft = act.getSupportFragmentManager().beginTransaction();
		     Fragment prev = act.getSupportFragmentManager().findFragmentByTag("cqd");
		     if (prev != null) {
		    	 ft.remove(prev);
		     }
		     ft.addToBackStack(null);
		     ft.commit();

		     // Create and show the dialog.
		     CurQuestDialog newFragment = CurQuestDialog.newInstance(mStackLevel, cb.getCalid(), cb.getEventid(), cb.getInstanceid(), CurrentQuests.this.qadapter, CurrentQuests.this.act, cb.getDifficulty());
		     newFragment.show(act.getSupportFragmentManager(), "cqd");
		 }
	};

}
